/**
 *  *2010-3-28 下午03:11:31  add 
 */
package com.base.myproject.client.busi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.HoursTable;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * @author Administrator 2010-3-28 javazgw@gmail.com
 */
public class ReservationX extends BaseJDBCPanel {
	Button Add = new Button("添加预约");
	Button refash = new Button("重新加载数据");
	
	DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy-M-d H:mm:ss");
	VerticalPanel v =new VerticalPanel();
	ContentPanel panel = new ContentPanel();
	public ReservationX() {
		super();
		init();
	}

	public void init() {
		busi = SystemMessage.busi[9];
		Add.addListener(Events.Select, getListener());
		refash.addListener(Events.Select, getListener());
		createHoursTableBcode();
		v.add(createGrid());
		v.add(HoursTableBcode_panel);
		// this.setAutoHeight(true);
		this.setAutoHeight(true);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(v, centerData);

		registerMainStore(store);
		registerStore(store);

		this.setBusino(busi);
		setdefault();

	}

	private void setdefault() {
		store.addDefault("busicode", busi);
		store.addDefault("bcode", Env.getEnvbcode());
		store.addDefault("busistatus", "1");

	}

	public void complete() {
		super.complete();
		// ttiratify.setText("提交确认");
		// getToolbarll().add(ttiratify);
		getToolbarll().clear();

		getToolbarll().add(Add);
		Add.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.addrecord()));
		getToolbarll().add(refash);
		refash.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.fresh()));
	}

	
	/*public ContentPanel initWeiget() {
		
		Add.addListener(Events.Select, getListener());
		refash.addListener(Events.Select, getListener());
		//panel.addButton(Add);
		panel.setLayout(new BorderLayout());
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));
		createHoursTableBcode();
		
		v.add(createGrid());
//		createGrid();
		v.add(HoursTableBcode_panel);
		
		
		panel.add(v, centerData);
		panel.setHeaderVisible(false);
		panel.setHeight("800");
		
//		panel.setAutoHeight(true);
		panel.setScrollMode(Scroll.AUTOY);
		return panel;
	}*/

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("reserdate");
		//type.getField("Reserdate").setType(Date.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		type.addField("prebcode");
		type.getField("prebcode").setType(String.class);
		type.addField("serviceitem");
		type.getField("serviceitem").setType(String.class);
		type.addField("predate");
		//type.getField("predate").setType(Date.class);
		type.addField("busistatus");
		type.getField("busistatus").setType(String.class);
		type.addField("s_mobil");
		type.getField("s_mobil").setType(String.class);
		type.addField("s_cardno");
		type.getField("s_cardno").setType(String.class);
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("deelhours");
		StoreInfo info = new StoreInfo();
		info.setTablename("Reservation");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select icode,(select bname from bcode where bcode.bcode = Reservation.bcode ) as bcode,ccode,reserdate,mark,prebcode,serviceitem,predate,busistatus,s_mobil,s_cardno,busicode ,deelhours from Reservation where DATEDIFF(day, getdate(), predate)=0");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

//		ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
//		columns.add(C1);
		
		ColumnConfig C11 = new ColumnConfig("s_cardno", "会员卡号", 80);
		columns.add(C11);
		ColumnConfig C10 = new ColumnConfig("s_mobil", "会员电话", 120);
		columns.add(C10);
		ColumnConfig C7 = new ColumnConfig("serviceitem", "预约项目", 80);
		columns.add(C7);
		ColumnConfig C2 = new ColumnConfig("bcode", "技师", 80);
		columns.add(C2);
		ColumnConfig C4 = new ColumnConfig("reserdate", "预约时间", 150);
		columns.add(C4);
		ColumnConfig C12 = new ColumnConfig("deelhours", "耗时", 50);
		columns.add(C12);

//		ColumnConfig C3 = new ColumnConfig("ccode", "会员", 80);
//		columns.add(C3);

		ColumnConfig C5 = new ColumnConfig("mark", "备注", 200);
		columns.add(C5);
//		ColumnConfig C6 = new ColumnConfig("prebcode", "制单人", 80);
//		columns.add(C6);
//
//		ColumnConfig C8 = new ColumnConfig("predate", "制单日期", 120);
//		columns.add(C8);
//		ColumnConfig C9 = new ColumnConfig("busistatus", "busistatus", 80);
//		columns.add(C9);
	


//		ColumnConfig C12 = new ColumnConfig("busicode", "busicode", 80);
//		columns.add(C12);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
//		grid.setAutoExpandColumn("s_cardno");
		grid.setSelectionModel(cbs);
		grid.setHeight(200);
		grid.setWidth(900);
//		grid.getView().setForceFit(true);
		
		return grid;
	}
	/**
	 * 当日技师预约一览表
	 * @return
	 */
	final VerticalPanel HoursTableBcode_panel =new VerticalPanel();;
	public void createHoursTableBcode()
	{
		//HoursTableBcode_panel = new VerticalPanel();
		//HoursTableBcode_panel.setWidth(900);
		 com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService.getDataSet("select Reserdate,deelhours,icode,(select bname from bcode where bcode.bcode = Reservation.bcode ) as bcode from reservation where datepart(Hour,Reserdate)>=8  and datediff(Day,getdate(),Reserdate)=0 ", getAsyncCallback());
			
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(Add)) {
								ReservationDialog rd = new ReservationDialog() {
									public void post() {
										close();
										aa();
										createHoursTableBcode();
									}
								};
								rd.show();
							}
							else 	if (be.getSource().equals(refash)) {
								store.load();
								createHoursTableBcode();
							}

						} else if (be.getType() == Events.Hide) {

						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("")) {

							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

	public void aa() {
		store.load();
	}
	
	HashMap<String,HoursTable> hm = new HashMap<String ,HoursTable>();
	private AsyncCallback<DataSet> getAsyncCallback()
	{
		
			AsyncCallback<DataSet> callback = new AsyncCallback<DataSet>()
	{

		@Override
		public void onFailure(Throwable caught) {
			MessageBox.alert("错误", "该错误发生于网络不稳定!请重新操作", null);
			
		}

		@Override
		public void onSuccess(DataSet result) {
			hm.clear();
		//	HoursTableBcode_panel = new VerticalPanel();
			HoursTableBcode_panel.removeAll();
			for(int i=0;i<result.getRowCount();i++)
			{
				String bcode = result.getValue(i, "bcode");
				if(!hm.containsKey(bcode))
				{
					hm.put(result.getValue(i, "bcode"), new HoursTable());
				}
				Date d = fmt.parse(result.getValue(i, "Reserdate"));
				
				String hours = result.getValue(i, "deelhours");
				double hour = (hours==null||hours.equals(""))?1:Double.parseDouble(hours);
				hour = hour*2;
				int d_hour = d.getHours();
				int d_min  = (d.getMinutes()==30)?1:0;
		
				
				GWT.log("hour="+hour+" d_hour="+d_hour+" d_min="+d_min+" ss"+(d_hour-8)*2,null);
				for(int j=0;j<hour;j++)
				{
					hm.get(bcode).setValue((d_hour-8)*2+d_min+j, result.getValue(i, "icode"), "");
				}
				
			}
			
			for(String key:hm.keySet())
			{
				HoursTableBcode_panel.add(new Html("<hr> "+key+":当日被预约时间表"));
				HoursTableBcode_panel.add(hm.get(key));
			}
			HoursTableBcode_panel.layout();
		//	HoursTableBcode_panel.layout(true);
			GWT.log(HoursTableBcode_panel+"--", null);
			v.layout();
			panel.layout();
			
		}
		
		
	};
	return callback;
	}
}
